/**
 * 
 */
package data;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class WordParser {

	final String subject;
	final String id;
	final String datetime;
	final String sender;
	final String body; 
	ArrayList<String> categories = null;
	
	/**
	 * @return the messages
	 */
	public static ArrayList<WordParser> getMessages() {
		return messages;
	}
	/**
	 * @return the subject
	 */
	public String getSubject() {
		return subject;
	}
	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}
	/**
	 * @return the datetime
	 */
	public String getDatetime() {
		return datetime;
	}
	/**
	 * @return the sender
	 */
	public String getSender() {
		return sender;
	}
	/**
	 * @return the body
	 */
	public String getBody() {
		return body;
	}

	private static ArrayList<WordParser> messages = new ArrayList<WordParser>();
	
	
	final static String FILENAME = "/Users/seanadams9/Documents/Eclipse Workspace/Visualization1/TestLabel.txt"; 
	
	public WordParser(String subject, String id, String datetime, String sender, String body){
		this.subject = subject;
		this.id = id;
		this.datetime = datetime;
		this.sender = sender;
		this.body = body;
		
	}
	
	public static void initialize(){
		
		
		String blah = System.getProperty("user.dir");
		System.out.println(blah);
		StringBuilder sb = getFileAsString(FILENAME);

		
		JSONArray jsonArray = JSONArray.fromObject(sb.toString()); 
		

		for(int i = 0; i < jsonArray.size(); i++){
			JSONObject emailObject = jsonArray.getJSONObject(i);
			String subject = emailObject.optString("subject");
			String id = emailObject.optString("id");
			String dateTime = emailObject.optString("datetime");
			String from = emailObject.optString("from");
			String body = emailObject.optString("body");
			WordParser wp = new WordParser(subject, id, dateTime, from, body);
			
			if(subject == null || id == null || dateTime == null || from == null || body == null)
				continue;
			messages.add(wp); 
			
		}
	}
	/**
	 * @return
	 */
	public static StringBuilder getFileAsString(String filename) {
		String array = "";
	    StringBuilder sb = new StringBuilder();
		try {
		    BufferedReader in = new BufferedReader(new FileReader(filename));
		    String str;
		    while ((str = in.readLine()) != null) {
		        sb.append(str); 
		    }
		    in.close();
		} catch (IOException e) {
		}
		return sb;
	}
	
	public static void main(String[] args){
		
		initialize();
		getMessages().size();
		
	}
}
